var netTool
var apiPath = 'capi/V2/ERorder/orderMain/'
var mDate = require("../tools/date")
function Order(_netTool) {
	netTool = _netTool
  // status
  // 订单状态：1，已下单未支付；100，已付款（待接单）；2，已分配；3，进行中；
  // 4，待评价；5，已完成；6，退款中；7，已退款; 8, 取消中；
  // 9, 取消；10已拒接; 11.待服务; 12. 待取货，13已评价, 14已投诉
  // 如果当前有未支付账单，则转入未支付账单，否则创建新订单
  this.save = function (data, fn) {
    netTool.getAjaxUrlValueBackground(apiPath + 'save', {
      ...data,
      owner_id: wx.getStorageSync('user_id'),
      status: 1,
    }, function (res) {
      typeof fn == 'function' && fn(res);
    })
  }
  /**
   * 1，已下单未支付；
   * @param {参数} data 
   * @param {回调} fn 
   */
  this.getNeedPay = function (data, fn) {
    netTool.getAjaxUrlValueBackground(apiPath + 'findById', {
      status: 1,
      ...data
    }, function (res) {
      typeof fn == 'function' && fn(res);
    })
  }
    //response_id 代表团长的id。如果要获得团长的团订单，可以通过团长id来查找
   this.list = function (data, fn) {
    netTool.getAjaxUrlValueBackground(apiPath + 'list', {
      // owner_id: wx.getStorageSync('user_id'),
      ...data
    }, function (res) {
      typeof fn == 'function' && fn(res);
    })
  }
  // 未完成订单
  this.getOrder = function (data, fn) {
    netTool.getAjaxUrlValueBackground(apiPath + 'findById', {
      // owner_id: wx.getStorageSync('user_id'),
      ...data
    }, function (res) {
      typeof fn == 'function' && fn(res);
    })
  }
  this.update = function (data, fn) {
    netTool.getAjaxUrlValueBackground(apiPath + 'update', {
      user_id: wx.getStorageSync('user_id'),
      ...data,
    }, function (res) {
      typeof fn == 'function' && fn(res);
    })
  }
  this.upOrder = function (data, fn) {
    // 取消的订单
    var that = this;
    netTool.getAjaxUrlValueBackground(apiPath + 'update', {
      user_id: wx.getStorageSync('user_id'),
      status: 3,
      ...data,
    }, function (res) {
      typeof fn == 'function' && fn(res);
    })
  }
  // 查询的订单
  this.findOrder = function (data, fn) {
    netTool.getAjaxUrlValueBackground(apiPath + 'list', {
      user_id: wx.getStorageSync('user_id'),
      ...data,
    }, function (res) {
      typeof fn == 'function' && fn(res);
    })
  }
  this.fresh = function (data, fn) {
    // page.currentPage=1
    this.findOrder(data, fn)
  }
  this.getMore = function (data, fn) {
    var data = {
      page: data.page + 1,
      total: data.total
    };
    var page = {
      page: data.page,
      status: data.status,
      rows: 3,
    }
    if (data.total >= data.page) {
      this.findOrder(page, fn)
    } else {
      console.log("没有了")
    }

  }
  this.updateById = function (data, fn) {
    netTool.getAjaxUrlValueBackground(apiPath + 'update', {
      owner_id: wx.getStorageSync('user_id'),
      id: data.id,
      total: data.total,
      address: data.roomId,
      remark: data.remark,
      real_amount: data.real_amount,
      contact_name: data.contact_name,
      contact_mobile: data.contact_mobile,
      amount: data.amount,
      order_no: data.order_no,
      pre_images: data.pre_images,
      status: 1
    }, function (res) {
      typeof fn == 'function' && fn(res);
    });
  }
  this.refund = function (id, fn) {
    netTool.getAjaxUrlValueBackground(apiPath + 'update', {
      id: id,
      status: 6,
      epprove_status:0
    }, function (res) {
      typeof fn == 'function' && fn(res);
    });
  }
  this.getNo = function () {
    const now = new Date()
    let month = now.getMonth() + 1
    let day = now.getDate()
    let hour = now.getHours()
    let minutes = now.getMinutes()
    let seconds = now.getSeconds()
    month = this.setTimeDateFmt(month)
    hour = this.setTimeDateFmt(hour)
    minutes = this.setTimeDateFmt(minutes)
    seconds = this.setTimeDateFmt(seconds)
    return now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math.round(Math.random() * 89 + 100)).toString()
  }
  this.setTimeDateFmt = function (d) {
    return d < 10 ? '0' + d : d
  }
}
module.exports = Order;